package cn.superchart.module.dev.gencode.service.impl;

import cn.superchart.common.base.BaseServiceImpl;
import cn.superchart.common.utils.Checker;
import cn.superchart.module.dev.gencode.entity.ScGenTable;
import cn.superchart.module.dev.gencode.mapper.ScGenTableMapper;
import cn.superchart.module.dev.gencode.param.*;
import cn.superchart.module.dev.gencode.service.ScGenTableService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class ScGenTableServiceImpl extends BaseServiceImpl<ScGenTable, ScGenTableMapper> implements ScGenTableService {

    @Autowired
    GenBuildToCodeService genBuildToCodeService;


    @Override
    public String add(ScGenTableAddParam scGenTableAddParam) {
          return super.saveData(scGenTableAddParam);
    }

    @Override
    public void edit(ScGenTableEditParam scGenTableEditParam) {
          super.updateData(scGenTableEditParam);
    }

    @Override
    public ScGenTableDetailParam detail(String id) {
        return queryData(id,ScGenTableDetailParam.class);
    }

    @Override
    public Page<ScGenTableDetailParam> page(ScGenTablePageParam scGenTablePageParam) {
        QueryWrapper<ScGenTable> queryWrapper =queryWrapper(scGenTablePageParam);
        Page<ScGenTable> page = super.page(scGenTablePageParam.defaultPage(),queryWrapper);
        Page<ScGenTableDetailParam> pageRes =copyProperties(page,ScGenTableDetailParam.class);
        return pageRes;
    }

    @Override
    public List<ScTableDetail> dbTables(String tableSchema) {
        List<ScTableDetail> scTableDetails = baseMapper.dbTables(tableSchema);
        return Checker.BeNotEmpty(scTableDetails)?scTableDetails: Lists.newArrayList();
    }

    @Override
    public List<ScTableColumnDetail> dbTableColumns(String tableSchema, String tableName) {
        List<ScTableColumnDetail> scTableColumnDetails = baseMapper.dbTableColumns(tableSchema,tableName);
        return Checker.BeNotEmpty(scTableColumnDetails)?scTableColumnDetails: Lists.newArrayList();
    }

    @Override
    public void createCode(String id) {
        ScGenTableDetailParam scGenTableDetailParam = queryData(id,ScGenTableDetailParam.class);
        genBuildToCodeService.build(scGenTableDetailParam);
    }


}
